//
//  PContentController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-01.
//

import UIKit

class PContentController: CJBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupData()
    }
    
    var menuView: CJMenuView!
    var collectionView: CJCollectionView!
    
    func setupData() {
        let datas = ["标题", "标题标题", "标题标题标题", "标题"]
        menuView.datas = datas
        menuView.reloadData()
        menuView.setupCurrent(value: 1, fromIndex: 0, toIndex: 0)
    }
    
    func setupView() {
        
        let config = CJMenuConfig()
        menuView = CJMenuView(frame: CGRect(x: 0, y: 0, width: view.cj_width, height: 40))
        config.direction = .horizontal
        config.isAverageItem = true
        self.view.addSubview(menuView)
        menuView.config = config
        menuView.didItemData = { menuView, index in
            let item = CJMenuTextItem()
            return item
        }
        menuView.didSelectItem = { [weak self] menuView, item, index in
            if let cv = self?.collectionView {
                cv.setContentOffset(CGPoint(x: Double(index) * cv.cj_width, y: 0.0), animated: true)
            }
        }
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        
        collectionView = CJCollectionView(frame: CGRect(x: 0, y: menuView.cj_bottom, width: view.cj_width, height: view.cj_height - menuView.cj_bottom), collectionViewLayout: layout)
        collectionView.type = 0
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        view.addSubview(collectionView)
        
        collectionView.register(PContentCell.self, forCellWithReuseIdentifier: "PContentCell")
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        collectionView.frame = CGRect(x: 0, y: menuView.cj_bottom, width: view.cj_width, height: view.cj_height - menuView.cj_bottom)
    }
}

extension PContentController {
    
    func setupCurrentCell(_ scrollView: UIScrollView) {
        let current = Int((scrollView.contentOffset.x/scrollView.cj_width))
        for index in 0..<self.menuView.datas.count {
            if let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? PContentCell {
                if index == current {
                    cell.current = current
                } else {
                    cell.current = nil
                }
            }
        }
    }
}

extension PContentController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == collectionView {
            let offsetx = scrollView.contentOffset.x
            menuView.setupCurrent(offset: offsetx, width: scrollView.cj_width, count: menuView.datas.count)
        }
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        // 代码滚动结束
        // kCJLog(scrollViewDidEndScrollingAnimation)
        setupCurrentCell(scrollView)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // 手动滚动结束
        // kCJLog(scrollViewDidEndDecelerating)
        setupCurrentCell(scrollView)
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menuView.datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PContentCell", for: indexPath)
        cell.backgroundColor = .cj_colorRandom
        if let cell = cell as? PContentCell {
            cell.index = indexPath.item
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.cj_width, height: collectionView.cj_height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
}
